# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=ambix
pkgname=(ambix ambix-{lv2,standalone,vst})
pkgver=0.2.10
pkgrel=4
pkgdesc="Ambisonic plug-ins with variable order for use in Digital Audio Workstations"
arch=(loong64 x86_64)
url="https://github.com/kronihias/ambix"
license=(GPL2)
makedepends=(
  alsa-lib
  cmake
  eigen
  fftw
  freetype2
  freeglut
  gcc-libs
  glibc
  gendesk
  glu
  libglvnd
  libsoxr
  libx11
  libxcursor
  libxext
  libxinerama
  jack
  liblo
  zita-convolver
)
checkdepends=(lv2lint)
source=(
  $url/archive/v$pkgver/$pkgname-v$pkgver.tar.gz
  $pkgname-0.2.10-lv2_plugin_uri.patch::$url/pull/28/commits/fa5849377b0eeddbedd1d99768cfc9ad9933d751.patch
  $pkgname-0.2.10-gcc9.1.0.patch::$url/pull/32/commits/30d9e8df4501d2dd5c35a09a14dd705413de44e7.patch
)
sha512sums=('6e53129bb258a7fd767fb60097354ead28cf05fd5e15017530518b5f0bc9f6350326a58ca81ab7250e70ae25b50900cb00092b489ae33974b258cb200f1c43f5'
            'f7d12a2983979b6545e9a03713e2088b30ec774a97d5abad7609ca2c2e9737635a30aad467b574a72eb02d2079007ebf281c3ed701f9e34864e0a865c12581ac'
            '35a9229bf6719952f49dd6cca5624c636a501877bd12c98724143f16cfd94514c3ed89e5c01ab08380c40c94cfc4506c6fe8dfdeed71bcbd1b152c3d0febd729')
b2sums=('957eef4181547cbfb25c74eaa262d837d904f0b77b026f78c94e9c479f489648a55c5c8d4952b1314fd5e1c0e57009f7b2e7e4e7f827275aed385ac60d554260'
        '633d38f8c5d94723a641cecd684fcff03eb0bdd26b9ea0b24566efa6ad788043a6deabfa18e7346ec304fb5fd6915cadc5952249ce49b0f8784e5afdb5f81c74'
        '4fea3e02c5a0c943afb82b69860b1baa3aa5afd94a474a5abfd781591705e4a4cc78a5425ee82b2b78e0fcfdd24599bc463c3ef1f8adf6b38e9999f9a65fe95a')

_plugin_names=(
  ambix_binaural
  ambix_converter
  ambix_decoder
  ambix_directional_loudness
  ambix_encoder_i2
  ambix_encoder_i4
  ambix_encoder_i6
  ambix_encoder_i8
  ambix_encoder
  ambix_maxre
  ambix_mirror
  ambix_rotator
  ambix_rotator_z
  ambix_vmic
  ambix_warp
  ambix_widening
)

_standalone_names=(
  ambix_binaural
  ambix_converter
  ambix_decoder
  ambix_directional_loudness
  ambix_encoder
  ambix_mirror
  ambix_rotator
  ambix_vmic
)

prepare() {
  # set proper LV2 URIs:
  # https://github.com/kronihias/ambix/issues/27
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-0.2.10-lv2_plugin_uri.patch
  # fix for gcc >= 9.1.0
  # https://github.com/kronihias/ambix/issues/26
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-0.2.10-gcc9.1.0.patch

  declare -A _descriptions=(
    ['ambix_binaural']="Listen to Ambisonics with Headphones"
    ['ambix_converter']="Convert between Ambisonics Formats"
    ['ambix_decoder']="Playback Ambisonics with Loudspeakers"
    ['ambix_directional_loudness']="Amplify, Attenuate or Filter Out Certain Parts of the Spherical Soundfield"
    ['ambix_encoder']="Encode Audio for Ambisonics"
    ['ambix_mirror']="Adjust Symmetric Components in Ambisonics Streams"
    ['ambix_rotator']="Rotate Sounds for Ambisonics"
    ['ambix_vmic']="Virtual Microphone: Select part of the Soundfield"
  )
  declare -A _generic=(
    ['ambix_binaural']="Binaural Decoder"
    ['ambix_converter']="Ambisonics Formats Converter"
    ['ambix_decoder']="Single-band Decoded Ambisonics"
    ['ambix_directional_loudness']="Spherical Soundfield Amplifier"
    ['ambix_encoder']="Ambisonics Panning"
    ['ambix_mirror']="Soundfield Mirroring"
    ['ambix_rotator']="Ambisonics Sounds Rotator"
    ['ambix_vmic']="Ambisonics Virtual Microphone"
  )
  for _standalone in "${_standalone_names[@]}"; do
    gendesk -n \
            --pkgname "${_standalone}" \
            --name "${_standalone}" \
            --pkgdesc "${_description[${_standalone}]}" \
            --genericname "${_generic[${_standalone}]}" \
            --categories "AudioVideo;Audio"
  done
}

build() {
  local _plugin_dir _plugin
  local cmake_options=(
    -B build
    -D BUILD_LV2=ON
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D OpenGL_GL_PREFERENCE=GLVND
    -D WITH_ZITA_CONVOLVER=ON
    -S $pkgname-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
  make -C $pkgname-$pkgver/lv2-ttl-generator
  for _plugin_dir in build/_bin/lv2/*.lv2; do
    (
      cd $_plugin_dir
      for _plugin in *.so; do
        "$srcdir/$pkgname-$pkgver/lv2-ttl-generator/lv2_ttl_generator" "./$_plugin"
      done
    )
  done
}

check() {
  for _plugin in "${_plugin_names[@]}"; do
    lv2lint -I build/_bin/lv2/${_plugin}_o5.lv2/ "https://github.com/kronihias/ambix/${_plugin}" || echo "Known to fail: https://github.com/kronihias/ambix/issues/29"
  done
}

package_ambix() {
  depends=(
    $pkgbase-{lv2,standalone,vst}
  )
}

package_ambix-lv2() {
  pkgdesc+=" - LV2 plugins"
  groups=(
    lv2-plugins
    pro-audio
  )
  depends=(
    fftw libfftw3f.so libfftw3f_threads.so
    freetype2 libfreetype.so
    gcc-libs
    glibc
    libglvnd
    libsoxr
    libx11
    libxext
    lv2-host
    zita-convolver libzita-convolver.so
  )

  for _plugin in "${_plugin_names[@]}"; do
    install -vDm 755 build/_bin/lv2/${_plugin}_o5.lv2/*.so -t "$pkgdir/usr/lib/lv2/${_plugin}_o5.lv2/"
    install -vDm 644 build/_bin/lv2/${_plugin}_o5.lv2/*.ttl -t "$pkgdir/usr/lib/lv2/${_plugin}_o5.lv2/"
  done
}

package_ambix-standalone() {
  pkgdesc+=" - standalone"
  groups=(
    pro-audio
  )
  depends=(
    alsa-lib libasound.so
    fftw libfftw3f.so libfftw3f_threads.so
    freetype2 libfreetype.so
    gcc-libs
    glibc
    libglvnd
    libsoxr
    libx11
    libxext
    zita-convolver libzita-convolver.so
  )
  
  for _standalone in "${_standalone_names[@]}"; do
    install -vDm 755 build/_bin/standalone/${_standalone}_standalone_o5 "$pkgdir/usr/bin/$_standalone"
  done
  install -vDm 644 ./*.desktop -t "$pkgdir/usr/share/applications/"
}

package_ambix-vst() {
  pkgdesc+=" - VST plugins"
  groups=(
    pro-audio
    vst-plugins
  )
  depends=(
    fftw libfftw3f.so libfftw3f_threads.so
    freetype2 libfreetype.so
    gcc-libs
    glibc
    libglvnd
    libsoxr
    libx11
    libxext
    vst-host
    zita-convolver libzita-convolver.so
  )

  for _plugin in "${_plugin_names[@]}"; do
    strip build/_bin/vst/${_plugin}_o5.so
    install -vDm 755 build/_bin/vst/${_plugin}_o5.so "$pkgdir/usr/lib/vst/$_plugin.so"
  done
}

